/**
 * 
 */
package com.cnc.bas.util;

import java.net.URL;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;



/**
 * @author axianchen
 *
 */
public class Log {

	private static final String LOG4J_CONFIG = "/log4j.xml";

	static {
		try {
			URL url = Log.class.getResource(LOG4J_CONFIG);
			System.out.println(url);
			DOMConfigurator.configure(url);			
			System.out.println("==== Init Log4j succeed ====");
		} catch (Throwable t) {
			System.err.println("==== Init Log4j failed, use basic config ====");
			t.printStackTrace();
			BasicConfigurator.configure();
		}
	}

	private Log() {

	}

	private Logger logger = null;

	private Log(Class clazz) {
		logger = Logger.getLogger(clazz);
	}

	public static Log getLogger(Class clazz) {
		return new Log(clazz);
	}

	/**
	 * dubug级别的log
	 */
	public void logDebug(Object message) {
		logger.log(Log.class.getName(), Level.DEBUG, message, null);
	}

	/**
	 * dubug级别的log, 包含出错信息
	 */
	public void logDebug(Object message, Throwable t) {
		logger.log(Log.class.getName(), Level.DEBUG, message, t);
	}

	/**
	 * info级别的log
	 */
	public void logInfo(Object message) {
		logger.log(Log.class.getName(), Level.INFO, message, null);
	}

	/**
	 * info级别的log, 包含出错信息
	 */
	public void logInfo(Object message, Throwable t) {
		logger.log(Log.class.getName(), Level.INFO, message, t);
	}

	/**
	 * warn级别的log
	 */
	public void logWarn(Object message) {
		logger.log(Log.class.getName(), Level.WARN, message, null);
	}

	/**
	 * warn级别的log, 包含出错信息
	 */
	public void logWarn(Object message, Throwable t) {
		logger.log(Log.class.getName(), Level.WARN, message, t);
	}

	/**
	 * error级别的log
	 */
	public void logError(Object message) {
		logger.log(Log.class.getName(), Level.ERROR, message, null);
	}

	/**
	 * error级别的log, 包含出错信息
	 */
	public void logError(Object message, Throwable t) {
		logger.log(Log.class.getName(), Level.ERROR, message, t);
	}

	/**
	 * fatal级别的log
	 */
	public void logFatal(Object message) {
		logger.log(Log.class.getName(), Level.FATAL, message, null);
	}

	/**
	 * fatal级别的log, 包含出错信息
	 */
	public void logFatal(Object message, Throwable t) {
		logger.log(Log.class.getName(), Level.FATAL, message, t);
	}
}
